home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / ANSI / c-client / os_dnv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  12.2 KB  |  471 lines

  1. /*
  2.  * Program:    Operating-system dependent routines -- MS-DOS (Novell) version
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    11 April 1989
  13.  * Last Edited:    15 June 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. /* TCP input buffer -- must be large enough to prevent overflow */
  37.  
  38. #define BUFLEN 8192
  39.  
  40.  
  41. /* TCP I/O stream (must be before osdep.h is included) */
  42.  
  43. #define TCPSTREAM struct tcp_stream
  44. TCPSTREAM {
  45.   char *host;            /* host name */
  46.   char *localhost;        /* local host name */
  47.   int tcps;            /* tcp socket */
  48.   long ictr;            /* input counter */
  49.   char *iptr;            /* input pointer */
  50.   char ibuf[BUFLEN];        /* input buffer */
  51. };
  52.  
  53.  
  54. /* Private function prototypes */
  55.  
  56. #include "mail.h"
  57. #include "osdep.h"
  58. #include <time.h>
  59. #include <sys\timeb.h>
  60. #include <sys\socket.h>
  61. #include <netinet\in.h>
  62. #include <netdb.h>
  63. #include "misc.h"
  64.  
  65.  
  66. /* Global data */
  67.  
  68. unsigned long rndm = 0xfeed;    /* initial `random' number */
  69.  
  70. /* Write current time in RFC 822 format
  71.  * Accepts: destination string
  72.  */
  73.  
  74. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  75.  
  76. void rfc822_date (char *date)
  77. {
  78.   time_t ti = time (0);
  79.   struct tm *t;
  80.   tzset ();            /* initialize timezone stuff */
  81.   t = localtime (&ti);        /* output local time */
  82.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %s",
  83.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  84.        t->tm_hour,t->tm_min,t->tm_sec,tzname[t->tm_isdst]);
  85. }
  86.  
  87. /* Get a block of free storage
  88.  * Accepts: size of desired block
  89.  * Returns: free storage block
  90.  */
  91.  
  92. void *fs_get (size_t size)
  93. {
  94.   void *block = malloc (size);
  95.   if (!block) fatal ("Out of free storage");
  96.   return (block);
  97. }
  98.  
  99.  
  100. /* Resize a block of free storage
  101.  * Accepts: ** pointer to current block
  102.  *        new size
  103.  */
  104.  
  105. void fs_resize (void **block,size_t size)
  106. {
  107.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  108. }
  109.  
  110.  
  111. /* Return a block of free storage
  112.  * Accepts: ** pointer to free storage block
  113.  */
  114.  
  115. void fs_give (void **block)
  116. {
  117.   free (*block);
  118.   *block = NIL;
  119. }
  120.  
  121.  
  122. /* Report a fatal error
  123.  * Accepts: string to output
  124.  */
  125.  
  126. void fatal (char *string)
  127. {
  128.   mm_fatal (string);        /* pass the string */
  129.   abort ();            /* die horribly */
  130. }
  131.  
  132. /* Copy string with CRLF newlines
  133.  * Accepts: destination string
  134.  *        pointer to size of destination string
  135.  *        source string
  136.  *        length of source string
  137.  */
  138.  
  139. char *strcrlfcpy (char **dst,unsigned long *dstl,char *src,unsigned long srcl)
  140. {
  141.   if (srcl > *dstl) {        /* resize if not enough space */
  142.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  143.     *dst = (char *) fs_get ((*dstl = srcl) + 1);
  144.   }
  145.                 /* copy strings */
  146.   if (srcl) memcpy (*dst,src,srcl);
  147.   *(*dst + srcl) = '\0';    /* tie off destination */
  148.   return *dst;            /* return destination */
  149. }
  150.  
  151.  
  152. /* Length of string after strcrlfcpy applied
  153.  * Accepts: source string
  154.  *        length of source string
  155.  */
  156.  
  157. unsigned long strcrlflen (STRING *s)
  158. {
  159.   return SIZE (s);        /* no-brainer on DOS! */
  160. }
  161.  
  162.  
  163. /* Return my home directory name
  164.  * Returns: my home directory name
  165.  */
  166.  
  167. char *hdname = NIL;
  168.  
  169. char *myhomedir ()
  170. {
  171.   int i;
  172.   char *s;
  173.   if (!hdname) {        /* get home directory name if not yet known */
  174.     hdname = cpystr ((s = getenv ("HOME")) ? s : "");
  175.     if ((i = strlen (hdname)) && ((hdname[i-1] == '\\') || (hdname[i-1]=='/')))
  176.       hdname[i-1] = '\0';    /* tie off trailing directory delimiter */
  177.   }
  178.   return hdname;
  179. }
  180.  
  181. /* TCP/IP open
  182.  * Accepts: host name
  183.  *        contact port number
  184.  * Returns: TCP/IP stream if success else NIL
  185.  */
  186.  
  187. TCPSTREAM *tcp_open (char *host,long port)
  188. {
  189.   TCPSTREAM *stream = NIL;
  190.   struct sockaddr_in sin;
  191.   struct hostent *host_name;
  192.   int sock;
  193.   long adr,i,j,k,l;
  194.   char *s;
  195.   char tmp[MAILTMPLEN];
  196.   char *hostname = host;
  197.                 /* set default gets routine */
  198.   if (!mailgets) mailgets = mm_gets;
  199.   /* The domain literal form is used (rather than simply the dotted decimal
  200.      as with other Unix programs) because it has to be a valid "host name"
  201.      in mailsystem terminology. */
  202.   sin.sin_family = AF_INET;    /* family is always Internet */
  203.                 /* look like domain literal? */
  204.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  205.     strcpy (tmp,host+1);    /* yes, copy number part */
  206.     tmp[strlen (tmp)-1] = '\0';
  207.     if ((sin.sin_addr.s_addr = inet_addr (tmp)) == -1) {
  208.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  209.       mm_log (tmp,ERROR);
  210.       return NIL;
  211.     }
  212.   }
  213.   else {            /* lookup host name */
  214.     if ((sin.sin_addr.s_addr = rhost (&hostname)) == -1) {
  215.       sprintf (tmp,"Host not found: %s",host);
  216.       mm_log (tmp,ERROR);
  217.       return NIL;
  218.     }
  219.   }
  220.  
  221.                 /* copy port number in network format */
  222.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  223.                 /* get a TCP stream */
  224.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  225.     sprintf (tmp,"Unable to create TCP socket (%d)",errno);
  226.     mm_log (tmp,ERROR);
  227.     return NIL;
  228.   }
  229.                 /* open connection */
  230.   if (connect (sock,(struct sockaddr *) &sin,sizeof (sin)) < 0) {
  231.     switch (errno) {        /* analyze error */
  232.     case ECONNREFUSED: s = "Refused"; break;
  233.     case ENO_RCB: s = "Insufficient system resources"; break;
  234.     case ETIMEDOUT: s = "Timed out"; break;
  235.     default: s = "Unknown error"; break;
  236.     }
  237.     sprintf (tmp,"Can't connect to %.80s,%ld: %s (%d)",hostname,port,s,errno);
  238.     mm_log (tmp,ERROR);
  239.     soclose (sock);
  240.     return NIL;
  241.   }
  242.                 /* create TCP/IP stream */
  243.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  244.                 /* official host name */
  245.   stream->host = cpystr (hostname);
  246.   adr = getmyipaddr ();        /* get local IP address */
  247.   i = adr >> 24; j = (adr >> 16) & 0xff; k = (adr >> 8) & 0xff; l = adr & 0xff;
  248.   sprintf (tmp,"[%ld.%ld.%ld.%ld]",i,j,k,l);
  249.   stream->localhost = cpystr (tmp);
  250.   stream->tcps = sock;        /* init socket */
  251.   stream->ictr = 0;        /* init input counter */
  252.   return stream;        /* return success */
  253. }
  254.   
  255. /* TCP/IP authenticated open
  256.  * Accepts: host name
  257.  *        service name
  258.  * Returns: TCP/IP stream if success else NIL
  259.  */
  260.  
  261. TCPSTREAM *tcp_aopen (char *host,char *service)
  262. {
  263.   return NIL;            /* always NIL on DOS */
  264. }
  265.  
  266. /* TCP/IP receive line
  267.  * Accepts: TCP/IP stream
  268.  * Returns: text line string or NIL if failure
  269.  */
  270.  
  271. char *tcp_getline (TCPSTREAM *stream)
  272. {
  273.   int n,m;
  274.   char *st,*ret,*stp;
  275.   char c = '\0';
  276.   char d;
  277.                 /* make sure have data */
  278.   if (!tcp_getdata (stream)) return NIL;
  279.   st = stream->iptr;        /* save start of string */
  280.   n = 0;            /* init string count */
  281.   while (stream->ictr--) {    /* look for end of line */
  282.     d = *stream->iptr++;    /* slurp another character */
  283.     if ((c == '\015') && (d == '\012')) {
  284.       ret = (char *) fs_get (n--);
  285.       memcpy (ret,st,n);    /* copy into a free storage string */
  286.       ret[n] = '\0';        /* tie off string with null */
  287.       return ret;
  288.     }
  289.     n++;            /* count another character searched */
  290.     c = d;            /* remember previous character */
  291.   }
  292.                 /* copy partial string from buffer */
  293.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  294.                 /* get more data from the net */
  295.   if (!tcp_getdata (stream)) return NIL;
  296.                 /* special case of newline broken by buffer */
  297.   if ((c == '\015') && (*stream->iptr == '\012')) {
  298.     stream->iptr++;        /* eat the line feed */
  299.     stream->ictr--;
  300.     ret[n - 1] = '\0';        /* tie off string with null */
  301.   }
  302.                 /* else recurse to get remainder */
  303.   else if (st = tcp_getline (stream)) {
  304.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  305.     memcpy (ret,stp,n);        /* copy first part */
  306.     memcpy (ret + n,st,m);    /* and second part */
  307.     fs_give ((void **) &stp);    /* flush first part */
  308.     fs_give ((void **) &st);    /* flush second part */
  309.     ret[n + m] = '\0';        /* tie off string with null */
  310.   }
  311.   return ret;
  312. }
  313.  
  314. /* TCP/IP receive buffer
  315.  * Accepts: TCP/IP stream
  316.  *        size in bytes
  317.  *        buffer to read into
  318.  * Returns: T if success, NIL otherwise
  319.  */
  320.  
  321. long tcp_getbuffer (TCPSTREAM *stream,unsigned long size,char *buffer)
  322. {
  323.   unsigned long n;
  324.   char *bufptr = buffer;
  325.   while (size > 0) {        /* until request satisfied */
  326.     if (!tcp_getdata (stream)) return NIL;
  327.     n = min (size,stream->ictr);/* number of bytes to transfer */
  328.                 /* do the copy */
  329.     memcpy (bufptr,stream->iptr,n);
  330.     bufptr += n;        /* update pointer */
  331.     stream->iptr +=n;
  332.     size -= n;            /* update # of bytes to do */
  333.     stream->ictr -=n;
  334.   }
  335.   bufptr[0] = '\0';        /* tie off string */
  336.   return T;
  337. }
  338.  
  339.  
  340. /* TCP/IP receive data
  341.  * Accepts: TCP/IP stream
  342.  * Returns: T if success, NIL otherwise
  343.  */
  344.  
  345. long tcp_getdata (TCPSTREAM *stream)
  346. {
  347.   fd_set fds;
  348.   FD_ZERO (&fds);        /* initialize selection vector */
  349.   if (stream->tcps < 0) return NIL;
  350.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  351.     FD_SET (stream->tcps,&fds);    /* set bit in selection vector */
  352.                 /* block and read */
  353.     if ((select (stream->tcps+1,&fds,0,0,0) < 0) ||
  354.     ((stream->ictr = soread (stream->tcps,stream->ibuf,BUFLEN)) < 1)) {
  355.       soclose (stream->tcps);    /* nuke the socket */
  356.       stream->tcps = -1;
  357.       return NIL;
  358.     }
  359.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  360.   }
  361.   return T;
  362. }
  363.  
  364. /* TCP/IP send string as record
  365.  * Accepts: TCP/IP stream
  366.  *        string pointer
  367.  * Returns: T if success else NIL
  368.  */
  369.  
  370. long tcp_soutr (TCPSTREAM *stream,char *string)
  371. {
  372.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  373. }
  374.  
  375.  
  376. /* TCP/IP send string
  377.  * Accepts: TCP/IP stream
  378.  *        string pointer
  379.  *        byte count
  380.  * Returns: T if success else NIL
  381.  */
  382.  
  383. long tcp_sout (TCPSTREAM *stream,char *string,unsigned long size)
  384. {
  385.   long i;
  386.   fd_set fds;
  387.   FD_ZERO (&fds);        /* initialize selection vector */
  388.   if (stream->tcps < 0) return NIL;
  389.   while (size > 0) {        /* until request satisfied */
  390.     FD_SET (stream->tcps,&fds);/* set bit in selection vector */
  391.     if ((select (stream->tcps+1,0,&fds,0,0) < 0) ||
  392.     ((i = sowrite (stream->tcps,string,size)) < 0)) {
  393.       soclose (stream->tcps);    /* nuke the socket */
  394.       stream->tcps = -1;
  395.       return NIL;
  396.     }
  397.     size -= i;            /* count this size */
  398.     string += i;
  399.   }
  400.   return T;            /* all done */
  401. }
  402.  
  403.  
  404. /* TCP/IP close
  405.  * Accepts: TCP/IP stream
  406.  */
  407.  
  408. void tcp_close (TCPSTREAM *stream)
  409. {
  410.                 /* nuke the socket */
  411.   if (stream->tcps >= 0) soclose (stream->tcps);
  412.   stream->tcps = -1;
  413.                 /* flush host names */
  414.   fs_give ((void **) &stream->host);
  415.   fs_give ((void **) &stream->localhost);
  416.   fs_give ((void **) &stream);    /* flush the stream */
  417. }
  418.  
  419. /* TCP/IP get host name
  420.  * Accepts: TCP/IP stream
  421.  * Returns: host name for this stream
  422.  */
  423.  
  424. char *tcp_host (TCPSTREAM *stream)
  425. {
  426.   return stream->host;        /* return host name */
  427. }
  428.  
  429.  
  430. /* TCP/IP get local host name
  431.  * Accepts: TCP/IP stream
  432.  * Returns: local host name
  433.  */
  434.  
  435. char *tcp_localhost (TCPSTREAM *stream)
  436. {
  437.   return stream->localhost;    /* return local host name */
  438. }
  439.  
  440. /* These functions are only used by rfc822.c for calculating cookies.  So this
  441.  * is good enough.  If anything better is needed fancier functions will be
  442.  * needed.
  443.  */
  444.  
  445.  
  446. /* Return host ID
  447.  */
  448.  
  449. unsigned long gethostid ()
  450. {
  451.   return (unsigned long) getmyipaddr ();
  452. }
  453.  
  454.  
  455. /* Return random number
  456.  */
  457.  
  458. long random ()
  459. {
  460.   return rndm *= 0xdae0;
  461. }
  462.  
  463.  
  464. /* Return `process ID'
  465.  */
  466.  
  467. long getpid ()
  468. {
  469.   return 1;
  470. }
  471.